/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.menu;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */
@ManagedBean(name="listMenu")
@SessionScoped

public class ListDinnerMenu implements Serializable {
    
     @Resource(name="jdbc/antonsDatasource")
     
             private DataSource ds;
    
   private DinnerMenu dinnerMenu;
   
   private String sql;
   
   
  // holds items extracted menuitems from database
  private ArrayList<DinnerMenu> menuList;
  
  //  used to hold selected  menuitems
  private ArrayList<DinnerMenu> selectedItems; 
  

  /*--------------------------------------------------------------------------*/
  // setter and getter for sql queries
    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
   
   /*-------------------------------------------------------------------------*/

    // setter and getter for  dinnerMenu
    public DinnerMenu getDinnerMenu() {
        return dinnerMenu;
    }

    public void setDinnerMenu(DinnerMenu dinnerMenu) {
        this.dinnerMenu = dinnerMenu;
    }
    /*------------------------------------------------------------------------*/
   
    // setter for datasource
    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    /*------------------------------------------------------------------------*/
    public ArrayList<DinnerMenu> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(ArrayList<DinnerMenu> selectedItems) {
        this.selectedItems = selectedItems;
    }
    

    /**
     *
     * @return
     * @throws SQLException
     */
         public ArrayList<DinnerMenu> getMenu() throws SQLException
             
     {  
         
          menuList = new ArrayList<>();
         
         if(ds==null)
			throw new SQLException("Can't get data source");// throws exception if datasource not available
       try (Connection con = ds.getConnection()) {
           if(con==null)
               throw new SQLException("Can't get database connection");
           
           PreparedStatement ps= con.prepareStatement(sql); // query select from FineMenu
                                
           
           //get  item info from database
           ResultSet result =  ps.executeQuery();
           
  
           while(result.next()){
               
               dinnerMenu=new DinnerMenu();
               
               dinnerMenu.setFineMenuId(result.getInt("finemenu_Id"));
               
               dinnerMenu.setFineMenuName(result.getString("finemenu_name"));
               
               //adds ingredients to list
               menuList.add(dinnerMenu);
           }
         
     
     return menuList;
     }
        
        
    }
    
}
